package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.behavior.service.BehaviorService;
import com.heima.common.constants.user.UserRelationConstants;
import com.heima.common.exception.CustomException;
import com.heima.model.behavior.dtos.ArticleBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
@Slf4j
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {
    @Autowired
    private BehaviorService behaviorService;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 查询文章行为相关信息
     */
    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDto dto) {
        //获取当前用户登录状态
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null || user.getId() == null) {
            CustomException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        //初始化值
        boolean isfollow = false, islike = false, isunlike = false, iscollection = false;
        Integer entryId = user.getId();
        Integer equipmentId = dto.getEquipmentId();
        Long articleId = dto.getArticleId();
        //判断当前数据库中有无行为实体类
        ApBehaviorEntry apBehaviorEntry = behaviorService.findApBehaviorEntryById(entryId, equipmentId);
        if (apBehaviorEntry == null) {
            CustomException.cust(AppHttpCodeEnum.PARAM_INVALID, "暂无行为实体类");
        }
        //获取查询条件
        Query query = Query.query(Criteria
                .where("entryId").is(apBehaviorEntry.getId())
                .and("articleId").is(articleId));
        //喜欢行为
        ApLikesBehavior likesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
        if (likesBehavior != null) {
            islike = true;
        }
        //不喜欢行为
        ApUnlikesBehavior unlikesBehavior = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
        if (unlikesBehavior != null) {
            isunlike = true;
        }
        //收藏行为
        ApCollection apCollection = mongoTemplate.findOne(query, ApCollection.class);
        if (apCollection != null) {
            iscollection = true;
        }
        //是否关注
        Double score = redisTemplate.opsForZSet().score(UserRelationConstants.FOLLOW_LIST + entryId, String.valueOf(dto.getAuthorApUserId()));
        if (score != null) {
            isfollow = true;
        }
        HashMap<String, Boolean> map = new HashMap<>();
        map.put("iscollection", iscollection);
        map.put("isfollow", isfollow);
        map.put("islike", islike);
        map.put("isunlike", isunlike);
        return ResponseResult.okResult(map);
    }
}
